<template>
  <div class="loginContainer">
    <h1 class="title">登录</h1>
    <el-page-header @back="goBack" content=""> </el-page-header>
    <img class="headImg" src="~assets/img/login/logo3.png" alt="" />
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item  label="用户名" prop="nickname">
        <el-input v-model="ruleForm.nickname" ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="success" plain @click="register">未注册?</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      callback();
    };
    
    return {
      ruleForm: {
        nickname: "",
        pass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  created() {
axios.get("/api/movie/top250").then((res) => {
                  res = res.data
                  if (res.errno === ERR_OK) {
                     this.themeList=res.data;
                  }
                }).catch((error) => {
                  console.warn(error)
                })
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = {
            nickname: this.ruleForm.nickname,
            password: this.ruleForm.pass
          };
          event.preventDefault();
          var result = axios({
            url: "http://localhost:3000/login",
            method: "post",
            data
          }).then(res => {
            let err_code = res.data.err_code;
            if (err_code === 0) {
              localStorage.setItem('status', res.data.session)
              this.$toast.show("登录成功", 1000);
              this.$router.push("/profile");
            } else if (err_code === 1) {
              this.$toast.show("信息错误", 1000);
            } else if (err_code === 500) {
              this.$toast.show("服务器正忙，稍后重试");
            }
          });
        } else {
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register() {
      this.$router.push({
        path: "/register"
      });
    },
    goBack() {
      this.$router.go(-1);
    }
  }
};
</script>

<style lang="css" scoped>
.loginContainer {
  position: relative;
  width: 100%;
  height: calc(100vh - 44px);
  background-color: #abdde8;
}
.demo-ruleForm {
  position: absolute;
  left: -20px;
}
.title {
  text-align: center;
  margin: 0;
}
.headImg {
   position: relative;
  margin: 10px 0;
  width: 100px;
  left: 50%;
  transform: translateX(-50%);
}
</style>
